import Vue from 'vue'
import Vuex from 'vuex'
import http from '@/assets/js/http'
import { Toast,Dialog  } from 'vant';
import util from '@/assets/js/util';
import {appid, formLoginClient} from '@/assets/js/config';

Vue.use(Vuex)

const store = new Vuex.Store({
	modules:{
		
	},
	state: {
		userInfo: {},
		fromAddress: null, // 用于下单页面，新增和编辑地址，返回下单页面的标识
		fromSalesman: null, // 用于分拨页面，新增和编辑业务员，返回分拨页面的标识
		currentPatient:{}, // 默认的就诊人
		initData:{}, // 字典列表,
		wcCode: null, // 微信登录的code
		openId: null,
	},
	mutations: {
		setUserInfo(state,data){
			state.userInfo = data;
		},
		setFromAddress(state,data){
			state.fromAddress = data;
		},
		setFromSalesman(state,data){
			state.fromSalesman = data;
		},
		setCurrentPatient(state,data){
			state.currentPatient = data;
			localStorage.setItem('currentPatient', JSON.stringify(data));
		},
		setInitData(state,data){
			state.initData = data;
		},
		setWxCode(state,data){
			state.wxCode = data;
		},
		setOpenId(state,data){
			state.openId = data;
		},
	},
	actions: {
		wechatLogin({commit,dispatch},code){
			return new Promise((resolve, reject) => {
				http('post', '/auth/oauth/token?mobile=VIPWX@'+code+'&grant_type=mobile&code=123456', {}, (data,msg) => {
					if(data.data.code == 1){
						Toast.fail(data.data.msg);
					}else{
						localStorage.setItem('access_token', data.data.access_token);
						Toast.success('登录成功！');
						util.gotoPage('/');
						resolve(data);
					}
				},msg => {
					Dialog.alert({
						title: '提示',
						message: msg,
					}).then(() => {
							
					});
					reject(msg);
				}, {headers:{Authorization : 'Basic ' + window.btoa(formLoginClient)}}, 'all');
			})
		},
		getWxCode({commit,dispatch}){
			let href = window.location.href;
			let code = dispatch('GetParam',href.split("#")[0], "code");
			if (!code) {
				// let url = encodeURIComponent('https://h5.hospital.caochuanxi.com#/login/index');
				let url = encodeURIComponent('https://h5.hospital.caochuanxi.com');
				window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+url+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
			} else {
				let res = dispatch('GetParam',href, "code");
				commit('setUserInfo', res);
			}
		},
		GetParam({commit,dispatch},url, code) {
			url = url + "";
			let regstr = "/(\\?|\\&)" + code + "=([^\\&]+)/";
			let reg = eval(regstr);
			//eval可以将 regstr字符串转换为 正则表达式
			let result = url.match(reg);
			if (result && result[2]) {
					return result[2];
			}
		},   
		getCurrentPatient({state,commit}){
			return new Promise((resolve, reject) => {
				if(state.currentPatient.patientId){
					resolve(state.currentPatient);
					// return;
				}else{
					http('get', '/admin/custApi/getPatientsList', {}, (data,msg) => {
						if(data.length == 0){
							commit('setCurrentPatient', {});
							localStorage.removeItem('currentPatient');						
						}else{
							let obj = data.find(item => item.isDefault == 1);
							if(obj){
								commit('setCurrentPatient', obj);
								localStorage.setItem('currentPatient', JSON.stringify(obj));
							}else{
								commit('setCurrentPatient', data[0]);
								localStorage.setItem('currentPatient', JSON.stringify(data[0]));	
							}
							
						}				
						resolve(data.records);					})
				}
			})
		},
		getUserInfo(context,payload){
			return new Promise((resolve, reject) => {
				http('get', '/lgg/agency/info',{},(data,msg) => {
					this.$store.commit('setUserInfo', data.data);
					resolve(data.data);
					// if(data.data.completeFlag == '0'){
					// 		this.$util.gotoPage('/');
					// }else{
					// 		this.$util.gotoPage('/user/perfect', data.data);
					// }
				});
			})
		},
		getPayCode({commit,dispatch},params){
			return new Promise((resolve, reject) => {
				http('post','/admin/registrationApi/submit/pay', params, (data,msg) => {
					resolve(data);
					// dispatch('submitPay',{...data, payId: data.payId}).then(res => {
					// 	util.payWx(res)
					// })
				})
			})
		},
		submitPay({commit,dispatch},params){
			return new Promise((resolve, reject) => {
				http('post','/admin/bill/submitPay', params, (data,msg) => {
					resolve(data);
				},msg => {
					reject(msg);
				})
			})
		},
		submitAndPay({commit,dispatch},params){
			return new Promise((resolve, reject) => {
				http('post','/admin/registrationApi/submit/pay', params, (dataCode,msg) => {
					http('post','/admin/bill/submitPay', {...dataCode, openId: params.openId}, (data,msg) => {
						resolve(data);
						util.payWx(data);
					},msg => {
						reject(msg);
					})
				},msg => {
					reject(msg);
				})

			});
		},
		submitAndPayGet({commit,dispatch},params){
			return new Promise((resolve, reject) => {
				http('post','/admin/registrationApi/submit/pay', params, (dataCode,msg) => {
					http('post','/admin/bill/submitPayGet', {...dataCode, openId: params.openId}, (data,msg) => {
						resolve(data);
						window.location.href = data;
					},msg => {
						reject(msg);
					})
				},msg => {
					reject(msg);
				})

			});
		}
		
	}
})

export default store
